package com.tiny.tinyweb.core.utils;

import java.util.Calendar;

public class DateUtil {

	/**
	 * 判断一天是否存在
	 * 
	 * @param year
	 * @param month
	 *            (0~11)
	 * @param day
	 * @return
	 */
	public static boolean IsDayValid(int year, int month, int day) {
		if (month > 11 || day < 1)
			return false;
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, 1);
		int MaxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 当前月最后一天

		return MaxDay >= day ? true : false;
	}

	/**
	 * 将Calendar星期转换成普通的星期
	 * 
	 * @param calendarWeek
	 * @return
	 */
	public static int ConvertCalendarWeekToLocal(int calendarWeek) {
		if (calendarWeek == 0)
			return 7;
		if (calendarWeek == 7)
			return 6;
		return calendarWeek-1;
	}

	/**
	 * 将普通的星期转换成Calendar星期
	 * 
	 * @param calendarWeek
	 * @return
	 */
	public static int ConvertLocalToCalendarWeek(int localWeek) {
		if (localWeek == 7)
			return 0;
		return localWeek + 1;
	}

	/**
	 * 获取一天是星期几
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static int getCurrentDayOfWeek(int year, int month, int day) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		return ConvertCalendarWeekToLocal(cal.get(Calendar.DAY_OF_WEEK));
	}

	/**
	 * 获取从今日起下一个星期nextWeek的日期,包含今天
	 * 
	 * @param year
	 * @param month
	 * @param currDay
	 * @param nextWeek
	 *            (1~7)
	 * @param hour 
	 * @return YYYY-MM-DD HH:MM:SS
	 */
	public static String getNextDayByWeek(int year, int month, int currDay,
			int nextWeek, int hour) {
		String nextWeekDay = "";
		int days=0;
		for (int i = 0; i < 7; i++) {
			if (!IsDayValid(year, month, currDay + days)) {
				if (month == 11) {
					year++;
					month = 0;
					currDay = 1;
					days=0;
				} else {
					month++;
					currDay = 1;
					days=0;
				}
			}
			int WeekOfDay = getCurrentDayOfWeek(year, month, currDay + days);
			if (WeekOfDay == nextWeek) {
				nextWeekDay = getDateStr(year,month+1,(currDay + days),hour);
				break;
			}
			days++;
		}
		return nextWeekDay;
	}

	/**
	 * 获取日期字符串
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @param hour
	 * @return yyyy-mm-dd hh:mm:ss
	 */
	public static String getDateStr(int year, int month, int day, int hour) {
		String monthStr = "";
		String dayStr = "";
		String hourStr = "";

		if (month < 10) {
			monthStr = "0" + month;
		} else {
			monthStr = "" + month;
		}
		if (day < 10) {
			dayStr = "0" + day;
		} else {
			dayStr = "" + day;
		}
		if (hour < 10) {
			hourStr = "0" + hour;
		} else {
			hourStr = "" + hour;
		}

		return year + "-" + monthStr + "-" + dayStr + " " + hourStr + ":00:00";

	}
	
	/**
	 * 
	 * @param date YYYY-MM-DD
	 * @return
	 */
	public static Calendar getCalendarByDateStr(String date){
		int year = Integer.parseInt(date.substring(0, 4));
		//月份-1
		int month = Integer.parseInt(date.substring(5, 7))-1;
		int day = Integer.parseInt(date.substring(8, 10));
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		return cal;
	}
}
